*------------------------------------------------------------------------------*
* Purpose: 			   Analysis for SL perception paper
* Dependent variables: D9.A (security), D9.B (ethnic relations), D9.D (freedom of speech)
*					   D9.C (gov. fairness)
* Estimation:		   Ordered logit, three categories from worse (=1) to better (=3)
*------------------------------------------------------------------------------*


*------------------
* Prepare working space
*------------------

set more off
clear

*findit parmest


*------------------
* Open cleaned and operationalized survey data
*------------------

use "SL_survey_analysis.dta", clear

	
*------------------	
* Set the data as survey data
*------------------
svyset sl_id 








*-------------------------------------------------------------------------------
* D9.B relationship bw different ethnic groups (5-point scale): 
*			1 = much worse
*			5 = much better
*-------------------------------------------------------------------------------


		*-------------------
		* Recode variable into three cagories: worse - same - better
		*-------------------
		
		recode d9_b_ethnic_rel_since_2009 (1/2=1) (3=2) (4/5=3), gen(d9_b_ethnic_rel_since_2009_3)
		tab d9_b_ethnic_rel_since_2009_3 
		
	*-------------
	* Full Model including media consumption
	*-------------
		#delimit ;
		svy:   	ologit
				d9_b_ethnic_rel_since_2009_3			
				a3_ethnic_sinhalese	
				a3_ethnic_tamil_moors 
				x4_gender 				
				a1_age
				age2
				education_low 
				education_high				
				a7_econ_own_condition
				a9_econ_own_vs_all
				e4_phys_harm_war
				c1_c2_consumption_traditional 	// Baseline: family 
				c1_c2_consumption_online
		;
		#delimit cr
		estimate store model_d9_b

		*-------------------
		* Predicted margins: Sinhalese = 1, all other variables to the mean 
		*-------------------
		
			* Outcome 1 = worse
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(1))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_b_sinhalese_o1
			parmest, saving("ethnicity_sinhalese_1.dta", replace) level(95) idstr("sinhalese_1")
			*
			quietly estimates restore model_d9_b
			
			
			* Outcome 2 = same
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(2))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_b_sinhalese_o2
			parmest, saving("ethnicity_sinhalese_2.dta", replace) level(95) idstr("sinhalese_2")

			*
			quietly estimates restore model_d9_b
			
			
			* Outcome 3 = better
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(3))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_b_sinhalese_o3
			parmest, saving("ethnicity_sinhalese_3.dta", replace) level(95) idstr("sinhalese_3")

			*
			quietly estimates restore model_d9_b
			
			
		*-------------------
		* Predicted margins: Tamil (all ethnics to = 0), all other variables to the mean 
		*-------------------
		
			* Outcome 1 = worse
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(1))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_b_tamil_o1
			parmest, saving("ethnicity_tamil_1.dta", replace) level(95) idstr("tamil_1")
			*
			quietly estimates restore model_d9_b
			
			
			* Outcome 2 = same
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(2))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_b_tamil_o2
			parmest, saving("ethnicity_tamil_2.dta", replace) level(95) idstr("tamil_2")
			*
			quietly estimates restore model_d9_b
			
			
			* Outcome 3 = better
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(3))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_b_tamil_o3
			parmest, saving("ethnicity_tamil_3.dta", replace) level(95) idstr("tamil_3")		
			*
			quietly estimates restore model_d9_b
			
			
		
		
		*------------------
		* Table the predicted probabilities: point estimates, CI in squared brackets
		*------------------		
		#delimit ;
		estout model_d9_b_sinhalese_o* model_d9_b_tamil_o*, cells(b(star fmt(%9.3f))   ci(par([  ,  ]) fmt(2)))  
								 starlevels(+ 0.10 * 0.05 ** 0.01)
								 legend 
								 label collabels(none) 
								 varlabels(_cons "Predicted Probability")
		;
		#delimit cr
		
	
	
		*------------------
		* Plot the predicted probabilities
		*------------------
		*Preserving data
               
			   preserve
               
               *Appending exported datasets with marginal effects 
               clear
               use "ethnicity_sinhalese_1.dta"
               append using "ethnicity_sinhalese_2.dta"
			   append using "ethnicity_sinhalese_3.dta"
			   append using "ethnicity_tamil_1.dta"
			   append using "ethnicity_tamil_2.dta"
			   append using "ethnicity_tamil_3.dta"
			   
              *Reshaping data
			   gen event = substr(idstr,-1,.)
			   destring event, replace
			   gen id = "Tamil" if regexm(idstr, "tamil")
			   replace id = "Sinhalese" if regexm(idstr, "sinhalese")
               keep estimate min* max* id event 

               
               *Figure: Ethnicity           
               #delimit ;                           
               twoway line estimate event if id == "Sinhalese", lcolor(gs0) lwidth(*.9)                                                                                                           
                                || rcap min95 max95 event if id == "Sinhalese", lcolor(gs0) lwidth(*1.15)
                                || scatter estimate event if id == "Sinhalese", msymbol(o) msize(*.65) mfcolor(white) mlcolor(gs0) mlwidth(*.8) mlalign(outside)                                                  
                   || line estimate event if id == "Tamil",  lcolor(ebblue) lwidth(*.9)  lp(shortdash)                                                                       
                                || rcap min95 max95 event if id == "Tamil", lcolor(ebblue) lwidth(*1.15)                                                       
                                || scatter estimate event if id == "Tamil", mcolor(ebblue) msize(*.65) lcolor(black)                                               
                     xsize(1.2) ysize(1) 
                     xscale(range(.75 3.25) extend titlegap(+2))
                     yscale(range() extend titlegap(+2))
                     ylabel(0(0.1).6 , valuelabel nogrid angle(0) labsize(*.80))
					 ymtick(##5)
                     xlabel(1 "Worse" 
							2 "Same"
							3 "Better", angle(horizontal) nogrid labsize(*.80))
                     xtitle("", size(*.75))
                     ytitle("Pr(Change in ethnic relations)", size(*.80))
                     legend(order( 3 "Sinhalese" 
								   6 "Tamil")                                                                         
                                   col(1)
                                   size(*.75)                                          
                                   keygap(*0.5) 
                                   symxsize(*0.35) 
                                   symysize(*0.5)
								   bm(zero)
                                   region(lcolor(gs0)) bm(l=12 r=3)
                                   position(2) ring(0) 
                                   )
					plotregion(lcolor(black)) 
                    graphregion(color(white) lcolor(white) lalign(outside))
					saving(ethnicity, replace)
                ; 
               #delimit cr                         

               *Restoring data
               restore


			   
			   
			   
			   
			   
			   
			   
			   
			   
			   
			   
			   
			   
			   
			   
			   
			   

	

*-------------------------------------------------------------------------------
* D9.A Overall security change since 2009 (5-point scale): 
*			1 = much worse
*			5 = much better
*-------------------------------------------------------------------------------


	*-------------------
	* Recode variable into three cagories: worse - same - better
	*-------------------
		
		recode d9_a_security_since_2009 (1/2=1) (3=2) (4/5=3), gen(d9_a_security_since_2009_3)
		tab d9_a_security_since_2009_3 
		
	*-------------
	* Full Model including media consumption
	*-------------
		#delimit ;
		svy:   	ologit
				d9_a_security_since_2009_3			
				a3_ethnic_sinhalese	
				a3_ethnic_tamil_moors 
				x4_gender 				
				a1_age
				age2
				education_low 
				education_high				
				a7_econ_own_condition
				a9_econ_own_vs_all
				e4_phys_harm_war
				c1_c2_consumption_traditional 	// Baseline: family 
				c1_c2_consumption_online
		;
		#delimit cr
		estimate store model_d9_a

		*-------------------
		* Predicted margins: Sinhalese = 1, all other variables to the mean 
		*-------------------
		
			* Outcome 1 = worse
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(1))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_a_sinhalese_o1
			parmest, saving("security_sinhalese_1.dta", replace) level(95) idstr("sinhalese_1")           
			*
			quietly estimates restore model_d9_a
			
			
			* Outcome 2 = same
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(2))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_a_sinhalese_o2
			parmest, saving("security_sinhalese_2.dta", replace) level(95) idstr("sinhalese_2")
			*
			quietly estimates restore model_d9_a
			
			
			* Outcome 3 = better
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(3))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_a_sinhalese_o3
			parmest, saving("security_sinhalese_3.dta", replace) level(95) idstr("sinhalese_3")
			*
			quietly estimates restore model_d9_a
			
			
		*-------------------
		* Predicted margins: Tamil (all ethnics to = 0), all other variables to the mean 
		*-------------------
		
			* Outcome 1 = worse
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(1))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_a_tamil_o1
			parmest, saving("security_tamil_1.dta", replace) level(95) idstr("tamil_1")
			*
			quietly estimates restore model_d9_a
			
			
			* Outcome 2 = same
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(2))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_a_tamil_o2
			parmest, saving("security_tamil_2.dta", replace) level(95) idstr("tamil_2")
			*
			quietly estimates restore model_d9_a
			
			
			* Outcome 3 = better
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(3))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_a_tamil_o3
			parmest, saving("security_tamil_3.dta", replace) level(95) idstr("tamil_3")
			*
			quietly estimates restore model_d9_a
			
			
			
	
		*------------------
		* Table the predicted probabilities: point estimates, CI in squared brackets
		*------------------		
		#delimit ;
		estout model_d9_a_sinhalese_o* model_d9_a_tamil_o*, cells(b(star fmt(%9.3f))   ci(par([  ,  ]) fmt(2)))  
								 starlevels(+ 0.10 * 0.05 ** 0.01)
								 legend 
								 label collabels(none) 
								 varlabels(_cons "Predicted Probability")
		;
		#delimit cr
		
	
	
		*------------------
		* Plot the predicted probabilities
		*------------------
		*Preserving data
               
			   preserve
               
               *Appending exported datasets with marginal effects 
               clear
               use "security_sinhalese_1.dta"
               append using "security_sinhalese_2.dta"
			   append using "security_sinhalese_3.dta"
			   append using "security_tamil_1.dta"
			   append using "security_tamil_2.dta"
			   append using "security_tamil_3.dta"
			   
              *Reshaping data
			   gen event = substr(idstr,-1,.)
			   destring event, replace
			   gen id = "Tamil" if regexm(idstr, "tamil")
			   replace id = "Sinhalese" if regexm(idstr, "sinhalese")
               keep estimate min* max* id event 

               
               *Figure: Security            
               #delimit ;                           
               twoway line estimate event if id == "Sinhalese", lcolor(gs0) lwidth(*.9)                                                                                                           
                                || rcap min95 max95 event if id == "Sinhalese", lcolor(gs0) lwidth(*1.15)
                                || scatter estimate event if id == "Sinhalese", msymbol(o) msize(*.65) mfcolor(white) mlcolor(gs0) mlwidth(*.8) mlalign(outside)                                                  
                   || line estimate event if id == "Tamil",  lcolor(ebblue) lwidth(*.9)  lp(shortdash)                                                                        
                                || rcap min95 max95 event if id == "Tamil", lcolor(ebblue) lwidth(*1.15)                                                       
                                || scatter estimate event if id == "Tamil", mcolor(ebblue) msize(*.65) lcolor(black)                                               
                     xsize(1.2) ysize(1) 
                     xscale(range(.75 3.25) extend titlegap(+2))
                     yscale(range() extend titlegap(+2))
                     ylabel(0(0.1).6 , valuelabel nogrid angle(0) labsize(*.80))
					 ymtick(##5)
                     xlabel(1 "Worse" 
							2 "Same"
							3 "Better", angle(horizontal) nogrid labsize(*.80))
                     xtitle("", size(*.80))
                     ytitle("Pr(Change in security)", size(*.80))
                     legend(off)
					plotregion(lcolor(black)) 
                    graphregion(color(white) lcolor(white) lalign(outside))
					saving(security, replace)
                ; 
               #delimit cr                         
               
               *Restoring data
               restore



	
	
	
	
	
	
	
	
	
	
	
	
		
		
*-------------------------------------------------------------------------------
* D9.D freedom of speech (5-point scale): 
*			1 = much worse
*			5 = much better
*-------------------------------------------------------------------------------


		*-------------------
		* Recode variable into three cagories: worse - same - better
		*-------------------
		
		recode d9_d_freedom_speech_since_2009 (1/2=1) (3=2) (4/5=3), gen(d9_d_freedom_speech_since_2009_3)
		tab d9_d_freedom_speech_since_2009_3 
		
	*-------------
	* Full Model including media consumption
	*-------------
		#delimit ;
		svy:   	ologit
				d9_d_freedom_speech_since_2009_3			
				a3_ethnic_sinhalese	
				a3_ethnic_tamil_moors 
				x4_gender 				
				a1_age
				age2
				education_low 
				education_high				
				a7_econ_own_condition
				a9_econ_own_vs_all
				e4_phys_harm_war
				c1_c2_consumption_traditional 	// Baseline: family 
				c1_c2_consumption_online
		;
		#delimit cr
		estimate store model_d9_d

		*-------------------
		* Predicted margins: Sinhalese = 1, all other variables to the mean 
		*-------------------
		
			* Outcome 1 = worse
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(1))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_d_sinhalese_o1
			parmest, saving("speech_sinhalese_1.dta", replace) level(95) idstr("sinhalese_1")
			*
			quietly estimates restore model_d9_d
			
			
			* Outcome 2 = same
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(2))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_d_sinhalese_o2
			parmest, saving("speech_sinhalese_2.dta", replace) level(95) idstr("sinhalese_2")
			*
			quietly estimates restore model_d9_d
			
			
			* Outcome 3 = better
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(3))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_d_sinhalese_o3
			parmest, saving("speech_sinhalese_3.dta", replace) level(95) idstr("sinhalese_3")
			*
			quietly estimates restore model_d9_d
			
			
		*-------------------
		* Predicted margins: Tamil (all ethnics to = 0), all other variables to the mean 
		*-------------------
		
			* Outcome 1 = worse
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(1))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_d_tamil_o1
			parmest, saving("speech_tamil_1.dta", replace) level(95) idstr("tamil_1")
			*
			quietly estimates restore model_d9_d
			
			
			* Outcome 2 = same
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(2))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_d_tamil_o2
			parmest, saving("speech_tamil_2.dta", replace) level(95) idstr("tamil_2")
			*
			quietly estimates restore model_d9_d
			
			
			* Outcome 3 = better
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(3))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_d_tamil_o3
			parmest, saving("speech_tamil_3.dta", replace) level(95) idstr("tamil_3")
			*
			quietly estimates restore model_d9_d
			
			
		
		
		*------------------
		* Table the predicted probabilities: point estimates, CI in squared brackets
		*------------------		
		#delimit ;
		estout model_d9_d_sinhalese_o* model_d9_d_tamil_o*, cells(b(star fmt(%9.3f))   ci(par([  ,  ]) fmt(2)))  
								 starlevels(+ 0.10 * 0.05 ** 0.01)
								 legend 
								 label collabels(none) 
								 varlabels(_cons "Predicted Probability")
		;
		#delimit cr
		

	
		*------------------
		* Plot the predicted probabilities
		*------------------
		*Preserving data
               
			   preserve
               
               *Appending exported datasets with marginal effects 
               clear
               use "speech_sinhalese_1.dta"
               append using "speech_sinhalese_2.dta"
			   append using "speech_sinhalese_3.dta"
			   append using "speech_tamil_1.dta"
			   append using "speech_tamil_2.dta"
			   append using "speech_tamil_3.dta"
			   
              *Reshaping data
			   gen event = substr(idstr,-1,.)
			   destring event, replace
			   gen id = "Tamil" if regexm(idstr, "tamil")
			   replace id = "Sinhalese" if regexm(idstr, "sinhalese")
               keep estimate min* max* id event 

               
               *Figure: Freedom of Speech            
               #delimit ;                           
               twoway line estimate event if id == "Sinhalese", lcolor(gs0) lwidth(*.9)                                                                                                           
                                || rcap min95 max95 event if id == "Sinhalese", lcolor(gs0) lwidth(*1.15)
                                || scatter estimate event if id == "Sinhalese", msymbol(o) msize(*.65) mfcolor(white) mlcolor(gs0) mlwidth(*.8) mlalign(outside)                                                  
                   || line estimate event if id == "Tamil",  lcolor(ebblue) lwidth(*.9) lp(shortdash)                                                                         
                                || rcap min95 max95 event if id == "Tamil", lcolor(ebblue) lwidth(*1.15)                                                       
                                || scatter estimate event if id == "Tamil", mcolor(ebblue) msize(*.65) lcolor(black)                                               
                     xsize(1.2) ysize(1) 
                     xscale(range(.75 3.25) extend titlegap(+2))
                     yscale(range() extend titlegap(+2))
                     ylabel(0(0.1).6 , valuelabel nogrid angle(0) labsize(*.80))
					 ymtick(##5)
                     xlabel(1 "Worse" 
							2 "Same"
							3 "Better", angle(horizontal) nogrid labsize(*.80))
                     xtitle("", size(*.75))
                     ytitle("Pr(Change in freedom of speech)", size(*.80))
                     legend(off)
					plotregion(lcolor(black)) 
                    graphregion(color(white) lcolor(white) lalign(outside))
					saving(speech, replace)
                ; 
               #delimit cr                         
               
               *Restoring data
               restore



			   
			   
			   
			   
			   
			   
			   
			   
*-------------------------------------------------------------------------------
* D9.C right to be treated faily by government and representatives (5-point scale): 
*			1 = much worse
*			5 = much better
*-------------------------------------------------------------------------------


		*-------------------
		* Recode variable into three cagories: worse - same - better
		*-------------------
		
		recode d9_c_gov_fair_since_2009 (1/2=1) (3=2) (4/5=3), gen(d9_c_gov_fair_since_2009_3)
		tab d9_c_gov_fair_since_2009_3
		
	*-------------
	* Full Model including media consumption
	*-------------
		#delimit ;
		svy:   	ologit
				d9_c_gov_fair_since_2009_3			
				a3_ethnic_sinhalese	
				a3_ethnic_tamil_moors 
				x4_gender 				
				a1_age
				age2
				education_low 
				education_high				
				a7_econ_own_condition
				a9_econ_own_vs_all
				e4_phys_harm_war
				c1_c2_consumption_traditional 	// Baseline: family 
				c1_c2_consumption_online
		;
		#delimit cr
		estimate store model_d9_c

		*-------------------
		* Predicted margins: Sinhalese = 1, all other variables to the mean 
		*-------------------
		
			* Outcome 1 = worse
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(1))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_c_sinhalese_o1
			parmest, saving("fair_sinhalese_1.dta", replace) level(95) idstr("sinhalese_1")
			*
			quietly estimates restore model_d9_c
			
			
			* Outcome 2 = same
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(2))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_c_sinhalese_o2
			parmest, saving("fair_sinhalese_2.dta", replace) level(95) idstr("sinhalese_2")
			*
			quietly estimates restore model_d9_c
			
			
			* Outcome 3 = better
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 1
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(3))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_c_sinhalese_o3
			parmest, saving("fair_sinhalese_3.dta", replace) level(95) idstr("sinhalese_3")
			*
			quietly estimates restore model_d9_c
			
			
		*-------------------
		* Predicted margins: Tamil (all ethnics to = 0), all other variables to the mean 
		*-------------------
		
			* Outcome 1 = worse
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(1))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_c_tamil_o1
			parmest, saving("fair_tamil_1.dta", replace) level(95) idstr("tamil_1")
			*
			quietly estimates restore model_d9_c
			
			
			* Outcome 2 = same
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(2))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_c_tamil_o2
			parmest, saving("fair_tamil_2.dta", replace) level(95) idstr("tamil_2")
			*
			quietly estimates restore model_d9_c
			
			
			* Outcome 3 = better
			#delimit ;
			margins,  at(a3_ethnic_sinhalese = 0
						 a3_ethnic_tamil_moors = 0
						 (means) _all
						 ) 		
					  predict(outcome(3))
					  contrast
					  post
			;
			#delimit cr
			estimates store model_d9_c_tamil_o3
			parmest, saving("fair_tamil_3.dta", replace) level(95) idstr("tamil_3")
			*
			quietly estimates restore model_d9_c
			
			
			
		
		*------------------
		* Table the predicted probabilities: point estimates, CI in squared brackets
		*------------------		
		#delimit ;
		estout model_d9_c_sinhalese_o* model_d9_c_tamil_o*, cells(b(star fmt(%9.3f))   ci(par([  ,  ]) fmt(2)))  
								 starlevels(+ 0.10 * 0.05 ** 0.01)
								 legend 
								 label collabels(none) 
								 varlabels(_cons "Predicted Probability")
		;
		#delimit cr
		
	
	
		*------------------
		* Plot the predicted probabilities
		*------------------
		*Preserving data
               
			   preserve
               
               *Appending exported datasets with marginal effects 
               clear
               use "fair_sinhalese_1.dta"
               append using "fair_sinhalese_2.dta"
			   append using "fair_sinhalese_3.dta"
			   append using "fair_tamil_1.dta"
			   append using "fair_tamil_2.dta"
			   append using "fair_tamil_3.dta"
			   
              *Reshaping data
			   gen event = substr(idstr,-1,.)
			   destring event, replace
			   gen id = "Tamil" if regexm(idstr, "tamil")
			   replace id = "Sinhalese" if regexm(idstr, "sinhalese")
               keep estimate min* max* id event 

               
               *Figure: Security            
               #delimit ;                           
               twoway line estimate event if id == "Sinhalese", lcolor(gs0) lwidth(*.9)                                                                                                           
                                || rcap min95 max95 event if id == "Sinhalese", lcolor(gs0) lwidth(*1.15)
                                || scatter estimate event if id == "Sinhalese", msymbol(o) msize(*.65) mfcolor(white) mlcolor(gs0) mlwidth(*.8) mlalign(outside)                                                  
                   || line estimate event if id == "Tamil",  lcolor(ebblue) lwidth(*0.9) lp(shortdash)                                                                         
                                || rcap min95 max95 event if id == "Tamil", lcolor(ebblue) lwidth(*1.15)                                                       
                                || scatter estimate event if id == "Tamil", mcolor(ebblue) msize(*.65) lcolor(black)                                               
                     xsize(1.2) ysize(1) 
                     xscale(range(.75 3.25) extend titlegap(+2))
                     yscale(range() extend titlegap(+2))
                     ylabel(0(0.1).6 , valuelabel nogrid angle(0) labsize(*.80))
					 ymtick(##5)
                     xlabel(1 "Worse" 
							2 "Same"
							3 "Better", angle(horizontal) nogrid labsize(*.80))
                     xtitle("", size(*.75))
                     ytitle("Pr(Change in gov. fairness)", size(*.80))
                     legend(off)
					plotregion(lcolor(black)) 
                    graphregion(color(white) lcolor(white) lalign(outside))
					saving(fair, replace)
                ; 
               #delimit cr                         
               
               *Restoring data
               restore	   
	   
   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
*-------------------------------------------------------------------------------
* Export table (Full models for peace, security, speech, and trust)
*-------------------------------------------------------------------------------

#delimit;													
	esttab 	model_d9_b
			model_d9_a
			model_d9_d
			model_d9_c
			using "table_1.tex", replace 				//former: Oslo_mainregtab_d9_b_a_d_c.tex
	b(3) se(3) 
	stats(	N,  fmt(0) 
			labels("Number of observations")
			) 
	order( 		a3_ethnic_sinhalese			
				a3_ethnic_tamil_moors 
				x4_gender 					
				a1_age
				age2
				education_low 
				education_high				
				a7_econ_own_condition
				a9_econ_own_vs_all
				e4_phys_harm_war
				c1_c2_consumption_traditional  
				c1_c2_consumption_online
			) 
	coeflabels( a3_ethnic_sinhalese				"Sinhalese"			
				a3_ethnic_tamil_moors 			"Tamil-Moor"
				x4_gender 						"Female"
				a1_age							"Age"
				age2							"Age\textsuperscript{2}"
				education_low 					"Low education"
				education_high					"High education"
				a7_econ_own_condition			"Absolute economic situation"
				a9_econ_own_vs_all				"Relative economic situation"
				e4_phys_harm_war				"Harmed in war"
				c1_c2_consumption_traditional  	"Use traditional media"
				c1_c2_consumption_online		"Use online media"
			) 
	label 
	star(\dag 0.10 * 0.05  ** 0.01 *** 0.001) 
	nonumber
	mgroups("\multicolumn{1}{c}{(1)}" 
			"\multicolumn{1}{c}{(2)}" 
			"\multicolumn{1}{c}{(3)}"
			"\multicolumn{1}{c}{(4)}"
			, pattern(1 1 1 1))
	nobaselevels 
	eqlabels(none)
	nomtitles
	nolegend 
	nonotes 
	booktabs 
	alignment(D{.}{.}{-1}) 
	compress
	substitute(\_ _)
	fragment
	prehead(
	\begin{table}[!ht]\centering
	\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
		\caption{Perceived changes in ethnic relations, security, freedom of speech, and political fairness since 2009 \label{tab:table1}}
		\setlength{\tabcolsep}{2pt}
		\scalebox{.85}{		
		\begin{tabular}{l*{4}{S}}
		\hline \hline	
		\addlinespace
		& \multicolumn{4}{c}{\textit{Dependent variable}}\\ 
		\cmidrule(r{5pt}l{5pt}){2-5} 
        \addlinespace
		& \multicolumn{1}{c}{Ethnic peace} & \multicolumn{1}{c}{Security} & \multicolumn{1}{c}{Speech} & \multicolumn{1}{c}{Fairness} \\
		)
		postfoot(
		\hline \hline
		\multicolumn{5}{l}{\footnotesize \textit{Note:} Coefficients with robust standard errors in parentheses.}\\
		\multicolumn{5}{l}{\footnotesize  $\dag$ p$<$0.1, * p$<$0.05, ** p$<$0.01, *** p$<$0.001.}\\
		\end{tabular}
		}
		\end{table}		
		);
#delimit cr













*-------------------------------------------------------------------------------
* Combine plots: D9.B D9.A, D9.D and D9.C
*-------------------------------------------------------------------------------

# delimit ;
gr combine ethnicity.gph security.gph speech.gph  fair.gph, 
	title("", 
	       ring(1) position(9) orientation(vertical) size(small) linegap(2)
		   margin(r=2))
	subtitle("", 
	       ring(1) position(6) orientation(horizontal) size(small) linegap(2)
		   margin(r=2))
	ysize(1) xsize(1)
	plotregion(margin(small) lc(black) ls(solid) lw(vthin) col(white)) 
	graphregion(col(white) lcolor(white) lalign(outside))
	scheme(s2mono)
	imargin(0 8 0 0)
;
#delimit cr

graph display, ysize(1) xsize(1) 
graph export "figure_2.pdf", as(pdf) replace			

erase ethnicity.gph 
erase security.gph 
erase speech.gph  
erase fair.gph











*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* Bivariate Analyses
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------


	*-------------
	* Bivariate model (D9.B relationship bw different ethnic groups )
	*-------------
		#delimit ;
		svy:   	ologit
				d9_b_ethnic_rel_since_2009_3			
				a3_ethnic_sinhalese	
				a3_ethnic_tamil_moors 
		;
		#delimit cr
		estimate store model_d9_b_biv

	
	*-------------
	* Bivariate Model (D9.A Overall security change since 2009)
	*-------------
		#delimit ;
		svy:   	ologit
				d9_a_security_since_2009_3			
				a3_ethnic_sinhalese	
				a3_ethnic_tamil_moors 
		;
		#delimit cr
		estimate store model_d9_a_biv


	*-------------
	* Bivariate Model (D9.D freedom of speech)
	*-------------
		#delimit ;
		svy:   	ologit
				d9_d_freedom_speech_since_2009_3			
				a3_ethnic_sinhalese	
				a3_ethnic_tamil_moors 
		;
		#delimit cr
		estimate store model_d9_d_biv		   
	
		
	*-------------
	* Bivariate Model (D9.C right to be treated faily by government and representatives) 
	*-------------
		#delimit ;
		svy:   	ologit
				d9_c_gov_fair_since_2009_3			
				a3_ethnic_sinhalese	
				a3_ethnic_tamil_moors 
		;
		#delimit cr
		estimate store model_d9_c_biv

		
		
		
*-------------------------------------------------------------------------------
* Export Appendix table A3
*-------------------------------------------------------------------------------

#delimit;													
	esttab 	model_d9_b_biv
			model_d9_b
			model_d9_a_biv
			model_d9_a
			model_d9_d_biv
			model_d9_d
			model_d9_c_biv
			model_d9_c
			using "Output\table_a3.tex", replace 				
	b(3) se(3) 
	stats(	N,  fmt(0) 
			labels("Number of observations")
			) 
	order( 		a3_ethnic_sinhalese			
				a3_ethnic_tamil_moors 
				x4_gender 					
				a1_age
				age2
				education_low 
				education_high				
				a7_econ_own_condition
				a9_econ_own_vs_all
				e4_phys_harm_war
				c1_c2_consumption_traditional  
				c1_c2_consumption_online
			) 
	coeflabels( a3_ethnic_sinhalese				"Sinhalese"			
				a3_ethnic_tamil_moors 			"Tamil-Moor"
				x4_gender 						"Female"
				a1_age							"Age"
				age2							"Age\textsuperscript{2}"
				education_low 					"Low education"
				education_high					"High education"
				a7_econ_own_condition			"Absolute economic situation"
				a9_econ_own_vs_all				"Relative economic situation"
				e4_phys_harm_war				"Harmed in war"
				c1_c2_consumption_traditional  	"Use traditional media"
				c1_c2_consumption_online		"Use online media"
			) 
	label 
	star(\dag 0.10 * 0.05  ** 0.01  *** 0.001) 
	nonumber
	mgroups("\multicolumn{1}{c}{(1)}" 
			"\multicolumn{1}{c}{(2)}" 
			"\multicolumn{1}{c}{(3)}"
			"\multicolumn{1}{c}{(4)}"
			"\multicolumn{1}{c}{(5)}" 
			"\multicolumn{1}{c}{(6)}" 
			"\multicolumn{1}{c}{(7)}"
			"\multicolumn{1}{c}{(8)}"
			, pattern(1 1 1 1 1 1 1 1))
	nobaselevels 
	eqlabels(none)
	nomtitles
	nolegend 
	nonotes 
	booktabs 
	alignment(D{.}{.}{-1}) 
	compress
	substitute(\_ _)
	fragment
	prehead(
	\begin{table}[!ht]\centering
	\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
		\caption{Perceived changes in ethnic relations, security, freedom of speech, and political fairness since 2009 \label{tab:table_rob_1234}}
		\setlength{\tabcolsep}{2pt}
		\scalebox{.7}{		
		\begin{tabular}{l*{8}{S}}
		\hline \hline	
		\addlinespace
		& \multicolumn{8}{c}{\textit{Dependent variable}}\\ 
		\cmidrule(r{5pt}l{5pt}){2-9} 
        \addlinespace
		& \multicolumn{2}{c}{Ethnic peace} & \multicolumn{2}{c}{Security} & \multicolumn{2}{c}{Speech} & \multicolumn{2}{c}{Fairness} \\ 
		\cmidrule(r{5pt}l{5pt}){2-3} \cmidrule(r{5pt}l{5pt}){4-5} \cmidrule(r{5pt}l{5pt}){6-7} \cmidrule(r{5pt}l{5pt}){8-9} 
		)
		postfoot(
		\hline \hline
		\multicolumn{9}{l}{\footnotesize \textit{Note:} Coefficients with robust standard errors in parentheses.}\\
		\multicolumn{9}{l}{\footnotesize  $\dag$ p$<$0.1, * p$<$0.05, ** p$<$0.01, *** p$<$0.001.}\\
		\end{tabular}
		}
		\end{table}		
		);
#delimit cr






*-------------------------------------------------------------------------------
** End of do file ** 
*-------------------------------------------------------------------------------
